<template>
  <div style="font-size: 14px;height: 100%;;background-color: #f5f5f5;">
    <div style="">
      <van-address-list
        v-model="chosenAddressId"
        :list="addressList"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="select"
      />
    </div>
  </div>
</template>

<script>
  import { getStore, setStore, removeStore } from "@/store/store.js";
  import { AddressList, Dialog } from 'vant';
  export default {

    components: {
      [AddressList.name]: AddressList,
      [Dialog.Component.name]: Dialog.Component,
    },

    data() {
      return {
        chosenAddressId: '',
        addressList: [],
        id: null
      };
    },
    mounted(){
      this.$store.commit('SET_HEADER_FOOTER', {showSearchHeader: true,headerName:'收货地址',pageName:'address',showFooter: false});
      this.getAddressList();
      let id = this.$route.query.id;
      if(id){//提交订单页面过来的
        this.id = id;
      }
    },
    methods: {
      //新增
      onAdd() {
        this.$router.push({path:'/addressEdit'})
      },
      //编辑
      onEdit(item, index) {
        this.$router.push({
          path:'/addressEdit',
          query:{
            id: item.id
          }
        })
      },
      getAddressList(){
        let addressList = getStore({name:'address_list'});
        if (addressList) {
          addressList.forEach(item=>{
            item['address'] = item.province+item.city+item.county+item.addressDetail;
          })
          this.addressList = addressList;
        }
      },
      //选择
      select(item,index){
        console.info(item)
        if(this.id){
          setStore({name:'address_pay',content: item});
          this.$router.go(-1);
        }
      }
    },
  };
</script>

<style scoped>
</style>
